Virtual data isn't strictly a 'part' of C++, however it can be simulated. It's not entirely pretty, but it works. First we'll cover what it is and how to simulate it, then conclude with why it isn't 'part' of C++.
Consider classes Vec (like an array of int) and SVec (a stretchable Vec; ie: SVec overrides operator[] to automatically stretch the number of elements whenever a large index is encountered). SVec inherits from Vec. Naturally Vec's subscript operator is virtual.
Now consider a VStack class (Vec-based-Stack). Naturally this Stack has a capacity limited by the fixed number of elements in the underlying Vec data structure. Then someone comes along and wants an SVStack class (SVec based Stack). For some reason, they don't want to merely modify VStack (say, because there are many users already using it).
The obvious choice then would be to inherit SVStack from VStack, however then there'd be *two* Vecs in an SVStack object (one explicitly in VStack, the other as the base class subobject in the SVec which is explicitly in the SVStack). That's a lot of extra baggage. There are at least 2 solns:
* break the is-a link between SVStack and VStack, text-copy the code from
VStack and manually change 'Vec' to 'SVec'.
* activate some sort of virtual data, so subclasses can change the class of subobjects.
To effect virtual data, we need to change the Vec subobject from a physically contained subobject into a ptr pointing to a dynamically allocated subobject:
Now the subclass has a shot at overriding the defn of the object referred to as 'v'. Ex: basically SVStack merely needs to bind a new SVec to 'v', rather than letting VStack bind the Vec. However classes can only initialize their *own* subobjects in an init-list. Even if I had used a ptr rather than a ref, VStack must be prevented from allocating its own 'Vec'. The way we do this is to add another ctor to VStack that takes a Vec& and does *not* allocate a Vec:
Cons: * extra layer of indirection to access the Vec
* extra freestore allocations (both new and delete)
* extra dynamic binding (reason given in next question)
We succeeded at making *our* job easier as implementor of SVStack, but all clients pay for it. It wouldn't be so bad if clients of SVStack paid for it, after all, they chose to use SVStack (you pay for it if you use it). However the 'optimization' made the users of the plain VStack pay as well!
See the question after the next to find out how much the client's 'pay'. Also: *PLEASE* read the few questions that follow the next one too (YOU WILL NOT GET A BALANCED PERSPECTIVE WITHOUT THE OTHERS).